﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;

namespace ScenarioFramework.Tools.UI.Controls.ListControls
{
    public partial class WorkflowList : UserControl
    {
        public XMLWorkflows xmlWorkflows;
        public XMLWorkflow XMLWorkflow;

        public WorkflowList()
        {
            InitializeComponent();
            this.MakeGrid();
        }

        private void MakeGrid()
        {
            ColumnHeader ch = new ColumnHeader();
            ch.Text = "ProcessId";
            ch.TextAlign = HorizontalAlignment.Right;
            ch.Width = 100;
            lwProcesses.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Name";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lwProcesses.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Description";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lwProcesses.Columns.Add(ch);

            ch = new ColumnHeader();
            ch.Text = "Status";
            ch.TextAlign = HorizontalAlignment.Left;
            ch.Width = 100;
            lwProcesses.Columns.Add(ch);
        }

        public ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses.XMLWorkflows XMLWorkflows
        {
            get { return xmlWorkflows; }
            set
            {
                xmlWorkflows = value;

                if (xmlWorkflows == null)
                    return;

                XMLWorkflow workflow = null;
                for (int i = 0; i < xmlWorkflows.Count; i++)
                {
                    workflow = (XMLWorkflow)xmlWorkflows[i];
                    ListViewItem item = new ListViewItem();
                    item.Text = workflow.ProcessId.ToString();
                    item.SubItems.Add(workflow.Name);
                    item.SubItems.Add(workflow.Description);
                    item.SubItems.Add(workflow.Status);
                    this.lwProcesses.Items.Add(item);
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.lwProcesses.Items.Clear(); 

            if (optOneProcess.Checked == true)
            {
                if (this.txtID.Text != string.Empty)
                {
                    this.LoadProcess(Int32.Parse(this.txtID.Text));
                }
                else if (this.ddlProcessTemplate.WorkflowName != string.Empty)
                {
                    MessageBox.Show("TODO");

                }
                else
                {
                    MessageBox.Show("It's mandatory select process Id or name");

                }
            }
            else
            {
                WAPI.Response wapiResponse = new WAPI.Response();
                string sXML = string.Empty;
                sXML = WAPI.Process.GetProcesses(ref wapiResponse);

                if (wapiResponse.iCode == 0)
                {
                    XMLWorkflows xmlWorkflows = new XMLWorkflows();
                    xmlWorkflows.Parse(sXML);

                    this.XMLWorkflows = xmlWorkflows;
                }
                else
                {
                    MessageBox.Show("Impossible to retrieve Processes List. " + wapiResponse.sMsg);
                }
            }
        }

        private void optOneProcess_CheckedChanged(object sender, EventArgs e)
        {
            if (optOneProcess.Checked == true)
            {
                this.txtID.Enabled = true;
                this.ddlProcessTemplate.Enabled = true;
            }
            else
            {
                this.txtID.Enabled = false;
                this.ddlProcessTemplate.Enabled = false;
            }
        }

        private void LoadProcess(long lProcessId)
		{
			WAPI.Response wapiResponse = new WAPI.Response();
			string sXML = string.Empty;
			sXML = WAPI.Process.GetProcessInstance ( ref wapiResponse, lProcessId);
			XMLWorkflow = new XMLWorkflow();
			XMLWorkflow.Parse (sXML);
		}

        private void lwProcesses_DoubleClick(object sender, EventArgs e)
        {
            this.LoadProcess(Int32.Parse(this.lwProcesses.SelectedItems[0].SubItems[0].Text));
        }

        private void mnuRestart_Click(object sender, EventArgs e)
        {
            if (this.lwProcesses.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select a process");
                return;
            }
            else if (this.lwProcesses.SelectedItems.Count > 1)
            {
                MessageBox.Show("Select only one process");
                return;
            }

            ;
            this.LoadProcess(Int32.Parse(this.lwProcesses.SelectedItems[0].SubItems[0].Text));

            //this.Close();

            if (this.XMLWorkflow != null)
            {
                Scenario.Forms.Workflow.ProcessInstanceManager instanceManager =
                    new Scenario.Forms.Workflow.ProcessInstanceManager();
                instanceManager.LoadProcess(ref this.XMLWorkflow);

                instanceManager.ShowDialog();
            }
        }

        private void mnuDetails_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TODO");
        }

			
    }
}
